import 'package:dmz/base/widget/defult_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class DateSelectWidget extends StatefulWidget {
  final String selectNow;
  final Function(String) finished;
  const DateSelectWidget(
      {super.key, required this.selectNow, required this.finished});

  @override
  State<DateSelectWidget> createState() => _DateSelectWidgetState();
}

class _DateSelectWidgetState extends State<DateSelectWidget> {
  late FixedExtentScrollController yearController;
  late FixedExtentScrollController mouthController;
  late FixedExtentScrollController dayController;

  int selectYear = 0;
  int selectMouth = 0;
  int selectDay = 0;

  int countYear = 3;
  int countMouth = 12;
  int countDay = 31;

  List<String> yearDatas = ['2024', '2023', '2022'];
  List<int> mouthed = [1, 3, 5, 7, 8, 10, 12];

  @override
  void initState() {
    var dates = widget.selectNow.split('-');
    selectYear = yearDatas.indexOf(dates[0]);
    selectMouth = int.parse(dates[1]) - 1;
    selectDay = int.parse(dates[2]) - 1;
    getDays(selectMouth + 1);

    yearController = FixedExtentScrollController(initialItem: selectYear);
    mouthController = FixedExtentScrollController(initialItem: selectMouth);
    dayController = FixedExtentScrollController(initialItem: selectDay);

    debugPrint('日期选择 ----- $selectDay $selectMouth $selectYear');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        item(yearController, countYear, selectYear, 0),
        item(mouthController, countMouth, selectMouth, 1),
        item(dayController, countDay, selectDay, 2),
      ]).marginOnly(left: 80.sp, right: 80.sp),
      DefaultBtn(click: () => widget.finished(getFinishedValue()), title: '确认')
          .marginOnly(bottom: 120.sp)
    ]);
  }

  item(FixedExtentScrollController controller, count, selectIndex, type) {
    return Container(
        color: Colors.transparent,
        child: Container(
            alignment: Alignment.center,
            color: Colors.transparent,
            width: 200.sp,
            height: 528.sp,
            child: ListWheelScrollView.useDelegate(
                itemExtent: 115.sp, // 设置单个项目的高度
                useMagnifier: true,
                magnification: 1.5,
                perspective: 0.01,
                squeeze: 1.0,
                overAndUnderCenterOpacity: 0.4,
                controller: controller,
                physics: const FixedExtentScrollPhysics(),
                onSelectedItemChanged: (index) => seleIndex(type, index),
                childDelegate: ListWheelChildBuilderDelegate(
                    childCount: count,
                    builder: (BuildContext context, int index) {
                      return Container(
                          alignment: Alignment.center,
                          color: Colors.transparent,
                          child: DefaultText(itemText(type, index), 40.sp,
                              textColor: selectIndex == index
                                  ? const Color(0xFF23C565)
                                  : const Color(0xCCF9F9F9)));
                    }))));
  }

  String itemText(int type, int index) {
    switch (type) {
      case 0:
        return yearDatas[index];
      case 1:
        return (index + 1).toString().padLeft(2, '0');
      case 2:
        return (index + 1).toString().padLeft(2, '0');
      default:
        return '';
    }
  }

  void seleIndex(int type, int index) {
    setState(() {
      switch (type) {
        case 0:
          selectYear = index;
          break;
        case 1:
          selectMouth = index;
          debugPrint('月份选择  -- $selectMouth');
          getDays(selectMouth + 1);
          break;
        case 2:
          selectDay = index;
          break;
        default:
      }
    });
  }

  void getDays(int mouth) {
    if (mouthed.contains(selectMouth + 1)) {
      countDay = 31;
    } else if (selectMouth == 2) {
      countDay = int.parse(yearDatas[selectYear]) % 4 == 0 ? 29 : 28;

      if (selectDay >= countDay) selectDay = countDay - 1;
    } else {
      countDay = 30;
      if (selectDay >= countDay) selectDay = countDay - 1;
    }
  }

  String getFinishedValue() {
    return '${yearDatas[selectYear]}-${(selectMouth + 1).toString().padLeft(2, '0')}-${(selectDay + 1).toString().padLeft(2, '0')}';
  }
}
